/**
 * JAVACC DEMO 1.0
 * @copy right zbxsoft company All rights reserved. 
 * @Package com.zbxsoft.platform.template.helper  
 */
package com.apache.design.template.helper;

import com.apache.design.template.FreemarkerHelper;
import com.apache.design.util.DatabaseUtils;
import com.apache.design.util.DesignEntity;
import com.apache.design.util.FileOperate;
import com.apache.design.util.GZipUtil;
import com.apache.info.entity.PubMetadata;
import com.apache.info.entity.PubSortInfo;
import com.apache.tools.StrUtil;

import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description:  生成其他实现类
 */
public class ManagerTempManager extends SupperTemplateManager {

	public boolean doInvoke(DesignEntity entity) {
		boolean mark = false;
		List<PubSortInfo> list = entity.getTabs();
		String jarPath = entity.getPackPath();
		String prefix = entity.getProjectName() + "/src/main/";
		String xmlPath = entity.getFilePath() + "/" + prefix + "java/" + jarPath.replace(".", "/");
		String sysName = entity.getSysEname();

		Connection conn = this.getConn(entity);
		try {
			createManager(conn, list, jarPath, prefix, xmlPath, sysName);
			createOther(jarPath, prefix, xmlPath, sysName);
			mark = true;
		} catch (Exception e) {
			e.printStackTrace();
			log.error("生成其他文件失败", e);
		} finally {
			DatabaseUtils.getInstance().close(conn,null);
		}
		return mark;
	}

	private void createManager(Connection conn, List<PubSortInfo> list, String jarPath, String projectName, String filePath,
			String sysName) throws Exception {
		FileOperate.getInstance().newCreateFolder(filePath + "/manager/");
		FileOperate.getInstance().newCreateFolder(filePath + "/service/impl/");
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("packPath", jarPath);
		data.put("reqName", sysName);
		data.put("sysName", GZipUtil.toUpperFirst(sysName));

		for (PubSortInfo map : list) {
			String objName = GZipUtil.toUpperFirst(map.getObjName());
			map.setObjName(objName);
			data.put("sort", map);
			String result = FreemarkerHelper.instance().getTemplateStr("manager/ObjNameManager.inc", data);
			FileOperate.getInstance().newCreateFile(filePath + "/manager/" + objName + "Manager.java", result);

			List<PubMetadata> mts = DatabaseUtils.getInstance().getTableColumns(conn, map.getInfoTableName());
			StringBuilder sbSave = new StringBuilder();
			if (!StrUtil.isEmpty(mts)) {
				for (int i = 0; i < mts.size(); i++) {
					PubMetadata mt = mts.get(i);
					if ("1".equals(String.valueOf(mt.getDataRestrict()))) {
						data.put("PrimaryAttr", GZipUtil.toUpperFirst(mt.getDataAttr()));
						data.put("PrimaryId", mt.getDataAttr());
						break;
					} else if ("4".equals(String.valueOf(mt.getDataRestrict()))) {
						sbSave.append("true");
					}
				}
				data.put("metadatas", mts);
				if(sbSave.length()>0)
				data.put("lhMark",sbSave.toString());
			}
			data.put("beanName", GZipUtil.toLowerFirst(map.getObjName()));
			result = FreemarkerHelper.instance().getTemplateStr("service/impl/ObjNameManagerImpl.inc", data);
			FileOperate.getInstance().newCreateFile(filePath + "/service/impl/" + objName + "ManagerImpl.java", result);
		}
	}

	private void createOther(String jarPath, String projectName, String filePath,
			String sysName) throws Exception {
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("packPath", jarPath);
		data.put("sysName", GZipUtil.toUpperFirst(sysName));
//		String result = FreemarkerHelper.instance().getTemplateStr("service/ServiceImpl.inc", data);
//		FileOperate.getInstance().newCreateFile(xmlPath + GZipUtil.toUpperFirst(sysName) + "ServiceImpl.java", result);

		String result = FreemarkerHelper.instance().getTemplateStr("manager/UnityBaseManager.inc", data);
		FileOperate.getInstance().newCreateFile(filePath + "/manager/" + "UnityBaseManager.java", result);

		FileOperate.getInstance().newCreateFolder(filePath + "/service/plugins/");
		result = FreemarkerHelper.instance().getTemplateStr("service/plugins/UnityPluginImpl.inc", data);
		FileOperate.getInstance().newCreateFile(
				filePath + "/service/plugins/" + GZipUtil.toUpperFirst(sysName) + "UnityPluginImpl.java", result);

		result = FreemarkerHelper.instance().getTemplateStr("service/impl/ProxyManagerImpl.inc", data);
		FileOperate.getInstance().newCreateFile(
				filePath + "/service/impl/" + GZipUtil.toUpperFirst(sysName) + "ProxyManagerImpl.java", result);

	}
}
